#ifndef __QUADTREE_H__
#define __QUADTREE_H__

#include "Infomation.h"
#include "Object.h"
#include <vector>

using std::vector;

#define LIM_W_H 5

class QuadNode
{
public:
	vector<Object*> listObj;

	RECT*		_rect;
	QuadNode*	LT,*RT,*LB,*RB;
	QuadNode()
	{
		_rect=NULL;
		LT=RT=LB=RB=NULL;
	}
	QuadNode(int left,int top,int width,int height)
	{
		_rect=new RECT();
		_rect->left=left;
		_rect->top=top;
		_rect->right=_rect->left+width;
		_rect->bottom=_rect->top+height;
		LT=RT=LB=RB=NULL;
	}
};

class QuadTree
{
public:
	bool CheckRECTInRECT(RECT r,RECT rect);
	bool CheckPointInRECT(int x,int y,RECT rect);
	void GetObjInRec(QuadNode* root,RECT Screen,vector<Object*> &result);

	QuadNode*	_pRoot;
	int			_mapWidth;
	int			_mapHeight;
	int			_count;
	QuadTree(int w,int h);
	~QuadTree(void);
	void Add(QuadNode *root,Object *Ob);
	void OutPutTree(QuadNode *root);
	//Chi in nhung cai nam tren main hinh hien tai
	void DrawObjects(int VirtualLeft,RECT Screen);
	Object* CheckCollision(QuadNode *root,RECT r,int Seq);
	void CheckCollision(QuadNode *root,RECT r,int Seq,vector<Object*>&result);
	bool IsOnObject(RECT *rect,int Seq);//duoi chan object la object khac hay la khoang trong
	void UpdatePosition(QuadNode *root);
	void GetAllObj(QuadNode *root,vector<Object*> &result);
	void RemoveAll(QuadNode *root);
};

#endif